const $ = (function () {
    const getParams = (obj) => {
        let str = "";
        for (var key in obj) {
            str += key + "=" + obj[key] + "&";
        }
        return str;
    }

    const jsonp = (settings) => {
        return new Promise(function (resolve, reject) {
            //1.随机生成一个函数名
            let fnName = "COOLMAN_" + parseInt(Math.random() * 1000);
            window[fnName] = resolve;
            
            //2.拼接url路径
            let newUrl = settings.url + "?" + getParams(settings.data) + settings.jsonpCallback + "=" + fnName;
            //3.创建script标签
            var oScript = document.createElement("script");
            document.body.appendChild(oScript);
            oScript.setAttribute("src", newUrl);

            oScript.onload = function () {
                this.remove();
            }
        })
    }
    return {
        jsonp
    }

})()
